// pages/collegeSign/collegeSign.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    values:[],
    type:2,
    qiandao:1,
    course_time_id:0,
    sign_type:0,
    page:1,
    course_date_list: [], //接收導航標籤頁的數據
    list: [], //接收学员列表數據
    course_content:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
   console.log('进入页面加载',e)
   var that = this
   that.setData({
    sign_type:e.sign_type,
    course_time_id:e.course_time_id
   })
   that.recommend_user()
  },//列表数据
  recommend_user:function(){
    var that = this
    getApp().request({
            url: getApp().api.schoolclass.class_student_list,
            method: "GET",
            data: {
               page:that.data.page,
               course_time_id:that.data.course_time_id
            },   
            success: function (res) {
               console.log(res, '数据123456')
              var course_content = res.data.course_content;
              that.setData({
                course_content:course_content
              })
              for(var i = 0;i<res.data.list.length;i++){
                that.data.list.push(res.data.list[i])
                that.setData({
                  list:that.data.list
                })
              }
              console.log(that.data.list,'/*/*/*/*/*//*/*/*/*/*/*//*/*')
            }
          });
  },
  qiandaos:function(e) {
   var that = this
    console.log(e,'eeeeeeeeeeee')
    var rid = e.currentTarget.dataset.rid
    var type = e.currentTarget.dataset.type
    var user_id = e.currentTarget.dataset.user_id
    //点击单个签到签退
    if (rid == "rid") {
      let user_ids = {
        user_id: parseInt(user_id),
      };
      that.data.values[0] = user_ids;
      console.log(that.data.values, "11111111111111111");
      that.data.qiandao = 2;
      // this.values = JSON.stringify(this.values);
      console.log(that.data.values, "22222222222222222");
      getApp().request({
              url: getApp().api.schoolclass.teacher_student_sign,
              method: "GET",
              data: {
                  course_time_id:that.data.course_time_id,
                  user_id_list:JSON.stringify(that.data.values),
                  type: type,
              },   
              success: function (res) {
               console.log(res, '数据')
             
                if(res.code == 0){
                that.setData({
                  list:[]
                })
                that.recommend_user()
                wx.showToast({
                  icon:'success',
                  title: res.msg,
                })
                }else {
                  wx.showToast({
                    icon:'error',
                    title: res.msg,
                  })
                }
                    
              }
            });
      //勾选多个签到签退
    } else {
      var that = this
      for (let i = 0; i < that.data.values.length; i++) {
        that.data.values[i] = {
          user_id: that.data.values[i],
        };
      }
      getApp().request({
              url: getApp().api.schoolclass.teacher_student_sign,
              method: "GET",
              data: {
                  course_time_id:that.data.course_time_id,
                  user_id_list:JSON.stringify(that.data.values),
                  type: type,
              },   
              success: function (res) {
               console.log(res, '数据')
                if(res.code == 0){
                  that.setData({
                    list:[]
                  })
                  that.recommend_user()
                  wx.showToast({
                    icon:'success',
                    title: res.msg,
                  })
                }else{
                  wx.showToast({
                    icon:'error',
                    title: res.msg,
                  })
                }
              }
            });
    }
  },
  qiandao:function(){
    this.setData({
      qiandao:2
    })
  },
  qiantui:function(){
    this.setData({
      qiandao:1
    })
  },


  guanli:function(){
    this.setData({
      type:1
    })
  },
  wancheng:function(){
    this.setData({
      type:2
    })
  },

  quanxuans:function(){
      for(let i =0;i<this.data.items;i++){
        this.data.items[i].checked = true
      }
  },
  checkboxChange(e) {
    console.log('checkbox发生change事件，携带value值为：',e, e.detail.value)
    // 全选取消全选
    if(e.currentTarget.dataset.type == "1"){
      // 取消全选
      if(this.data.values.length > 0){
        for(let i =0;i<this.data.list.length;i++){
          this.data.values.push(this.data.list[i].user_id)
        }
        this.setData({
          values:this.data.values
        })
        console.log(this.data.values, '*********************')
        const list = this.data.list
       
        for (let i = 0, lenI = list.length; i < lenI; ++i) {
          list[i].checked = false
    
          for (let j = 0, lenJ = this.data.values.length; j < lenJ; ++j) {
            if (list[i].user_id ===  this.data.values[j]) {
              list[i].checked = false
              break
            }
          }
        }
    
        this.setData({
          list,
          values:[]
        })
      }else{
        // 全选
        var arr = this.data.list
        if(arr.every( item => item.course_sign_in_time > 0) || arr.every( item => item.course_sign_in_time === 0)){
          console.log('验证通过')
          for(let i =0;i<arr.length;i++){
            this.data.values.push(arr[i].user_id)
        
          }
          this.setData({
            values:this.data.values
          })
          console.log(this.data.values, '*********************')
          const list = this.data.list
         
          for (let i = 0, lenI = list.length; i < lenI; ++i) {
            list[i].checked = false
      
            for (let j = 0, lenJ = this.data.values.length; j < lenJ; ++j) {
              if (list[i].user_id ===  this.data.values[j]) {
                list[i].checked = true
                break
              }
            }
          }
      
          this.setData({
            list
          })
        }else {
          console.log('验证不通过')
        }
      
      }
 
    }else{
      // 单个选中
      const list = this.data.list
      const values = e.detail.value
      this.setData({
        values:e.detail.value
      })
      for (let i = 0, lenI = list.length; i < lenI; ++i) {
        list[i].checked = false
  
        for (let j = 0, lenJ = values.length; j < lenJ; ++j) {
          if (list[i].user_id === values[j]) {
            list[i].checked = true
            break
          }
        }
      }
      this.setData({
        list
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this
    that.setData({
      page:that.data.page + 1
    })
    that.recommend_user()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})